package com.cnzhizi.wheelpop.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.cnzhizi.wheelpop.R;
import com.cnzhizi.wheelpop.model.OneModel;
import com.cnzhizi.wheelpop.model.ThreeModel;
import com.cnzhizi.wheelpop.model.TwoModel;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ListWheelAdapter;

/**
 * Created by Sun on 2016/7/15.
 */
public class SkmDatePickerPopup extends PopupWindow  {
    private static int year;
    private static int month;
    private static int day;
    private static int hour;
    private static int minute;

    private Button mBtnConfirm;
    private TextView tv_title;
    private DatePicker datePicker;
    private TimePicker timePicker;

    private String title="请选择";


    private Context context;
    private SkmDatePickerPopupOnClickListener onClickListener;

    boolean isShouledChangeDate=false;
    long targetDate;
    private View rootView;
    static View onclickView;//外部点击的
    /**
     *
     * @param context
     * @param onClickListener
     * @param title
     */
    public SkmDatePickerPopup(Context context, SkmDatePickerPopupOnClickListener onClickListener, String title,View onclickView){
        super(context);
        this.context=context;
        this.onClickListener=onClickListener;

        this.title=title;
        SkmDatePickerPopup.onclickView =onclickView;


        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.activity_datepicker, null);

        initViews();
        initListener();
        initData();


        // 设置SelectPicPopupWindow的View
        this.setContentView(rootView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x80000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        rootView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View v, MotionEvent event) {

                int height = rootView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }
    //添加两个参数 是否需要更改默认当前时间 isShouledChangeDate 更改后的时间 targetDate
    public SkmDatePickerPopup(Context context, SkmDatePickerPopupOnClickListener onClickListener, String title,View onclickView,boolean isShouledChangeDate,long targetDate ){
        super(context);
        this.context=context;
        this.onClickListener=onClickListener;
        this.isShouledChangeDate=isShouledChangeDate;
        this.targetDate=targetDate;

        this.title=title;
        SkmDatePickerPopup.onclickView =onclickView;


        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.activity_datepicker, null);

        initViews();
        initListener();
        initData();


        // 设置SelectPicPopupWindow的View
        this.setContentView(rootView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x80000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        rootView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View v, MotionEvent event) {

                int height = rootView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

    private void initListener() {
        // 添加onclick事件
        mBtnConfirm.setOnClickListener(onClickListener);
    }

    private void initViews() {

        mBtnConfirm = (Button) rootView.findViewById(R.id.btn_confirm);
        tv_title = (TextView) rootView.findViewById(R.id.tv_title);

        datePicker = (DatePicker) rootView.findViewById(R.id.datePicker);
        timePicker = (TimePicker) rootView.findViewById(R.id.timePicker);
        timePicker.setIs24HourView(true);//24小时制
    }
    private void initData() {
        tv_title.setText(title);
      //  获取当前的年、月、日、小时、分钟
        Calendar c = Calendar.getInstance();
        if (isShouledChangeDate){//需要改变默认值
            c.setTimeInMillis(targetDate);
        }

        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        hour = c.get(Calendar.HOUR_OF_DAY);//24小时制
        minute = c.get(Calendar.MINUTE);
        // 初始化DatePicker组件，初始化时指定监听器
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener()
        {

            @Override
            public void onDateChanged(DatePicker arg0, int year
                    , int month, int day)
            {
                SkmDatePickerPopup.year = year;
                SkmDatePickerPopup.month = month;
                SkmDatePickerPopup.day = day;
                // 显示当前日期、时间
             //   showDate(year, month, day, hour, minute);

            }
        });
        // 为TimePicker指定监听器
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
        {

            @Override
            public void onTimeChanged(TimePicker view
                    , int hourOfDay, int minute)
            {
                hour = hourOfDay;
                SkmDatePickerPopup.minute = minute;
                // 显示当前日期、时间
              //  showDate(year, month, day, hour, minute);
            }
        });


    }


    public  static    abstract class SkmDatePickerPopupOnClickListener implements View.OnClickListener{
        public SkmDatePickerPopupOnClickListener() {
        }

        @Override
        public void onClick(View v) {

            myOnClick(year,month+1,day,hour,minute, v,onclickView);
        }
        public abstract void myOnClick(int year,int month,int day,int hour,int minute, View v,View root);
    }

}
